<?php
    class menutype extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->model('admin/menutype_model');
        }
        public function index()
        {
            $data=$this->menutype_model->getAll(10,0,-1);
            $this->load->vars('item',$data);
        }
        
        //add new article
        public function add()
        {
            $category=$this->menutype_model->getAll(10,0,1);
            $this->load->vars('category',$category);
        }
        
        //edit article 
        public function edit()
        {
            $id=$this->uri->segment(4);
            $article=$this->menutype_model->getmenutypeById($id);
            $this->load->vars('item',$article);
        }
        
        //save new article or update 
        public function save()
        {
            if ($this->form_validation->run('save') == FALSE)
            {
                $this->form_validation->set_error_delimiters('<div class="error" style="text-align:center;">', '</div>');
                $this->load->view('admin/form/form');
            }
            else
            {
                $post=$this->input->post();
                $id=$post['id'];
                $name=$post['name'];
                $alias=$post['alias'];
                $publish=$post['publish'];
                $order=$post['order'];
                $data=array();
                $data['menutype_title']=$name;
                $data['menutype_alias']=$alias;
                $data['menutype_order']=$order;
                $data['menutype_publish']=$publish;
                //call save
                if($this->menutype_model->save($id,$data)>0)
                {
                    
                   
                } 
                 redirect(base_url()."admin/menutype");
            }
           
            
        }
        //publish article
        public function publish()
        {
            $id=$this->uri->segment(4);
            $this->menutype_model->publish(1,array($id));
            redirect(base_url()."admin/menutype");
        }
        //unpublish
        public function unpublish()
        {
            $id=$this->uri->segment(4);
            $this->menutype_model->publish(0,array($id));
            redirect(base_url()."admin/menutype");
        }
        //for actions from menutype
        
        public function action()
        {
            $task=$this->input->post('task');
            $task=trim($task);
            $id=$this->input->post('id');
            switch($task)
            {
                case 'saveorder':
                $cid=$this->input->post('cid');
                $order=$this->input->post('order');
                $this->menutype_model->saveorder($cid,$order);
                break;
                case 'delete':
                $this->menutype_model->delete($id);
                break;
                case 'publish':
                $this->menutype_model->publish(1,$id);
                break;
                case 'unpublish':
                $this->menutype_model->publish(0,$id);
                break;
                
            }
            redirect(base_url()."admin/menutype");
        }
    }